require.config({baseUrl: '/js/lib',urlArgs: "v0.0.1"});
require(["zepto", "lib", 'swiper'], function($, lib, swiper){
  lib = new lib();
  var fn = {};
  $.extend(fn, {
    goodsId: lib.getUrlParam('gId'),
    storageId: lib.getUrlParam('sId'),
    init: function(){
      
      this.initPage();
      this.bindEvent();
      this.isConllected();
      lib.getCartNum();
    },
    initBanner: function(){
      new swiper('.ui-banner', {
        autoplay: 5000, //可选选项，自动滑动
        pagination : '.swiper-pagination',
      });
    },
    bindEvent: function(){
      var _t = this;
      /*商品加减操作*/
      $(document).on('tap', '.ui-g-num > i', function(){
        var goodsNum = Number($('input[name=goodsNum]').val());
        if($(this).attr("class") === 'ui-add'){
          goodsNum < 99 ? $('input[name=goodsNum]').val(++goodsNum) : '';
        }else{
          goodsNum > 1 ? $('input[name=goodsNum]').val(--goodsNum) : '';
        }
      });
      $(document).on('blur', 'input[name=goodsNum]', function(){
        var goodsNum = Number($('input[name=goodsNum]').val());
        if(isNaN(goodsNum)){
          goodsNum = 1
        }else if(goodsNum > 99){
          goodsNum = 99;
        }else if(goodsNum < 1){
          goodsNum = 1;
        }
        $('input[name=goodsNum]').val(goodsNum);
      });
      /*加入购物车*/
      $(document).on('tap', '.ui-add-cart', function(){
        lib.ajx(lib.getReq().ser + 'shoppingCart/addToCart.action', {goodsId: _t.goodsId, storageId: _t.storageId, goodsNum: $('input[name=goodsNum]').val()}, function(data){
          lib.showMessage(data.info);
        }, function(){
          lib.showMessage('网络延时，请稍后再试');
        });
      });
      
      /*加入收藏*/
      $(document).on('tap', '.ui-add-collect', function(){
        var _this = this;
        if($(_this).hasClass('active')){
          lib.ajx(lib.getReq().ser + 'memberCollectGoods/deleteCollections.action', {mcgIds: [$(_this).attr('data-cId')]}, function(data){
            if(data.infocode === "2") location.href = `login.html?backUrl=${lib.getBackUrl()}`;
            if(data.infocode === "0"){
              $(_this).removeClass('active');
              return;
            } 
            lib.showMessage(data.info);
          }, function(){
            lib.showMessage('网络延时，请稍后再试');
          });
        }else{
          lib.ajx(lib.getReq().ser + 'memberCollectGoods/collecting.action', {goodsId: _t.goodsId, storageId: _t.storageId}, function(data){
            if(data.infocode === "2") location.href = `login.html?backUrl=${lib.getBackUrl()}`;
            if(data.infocode === "0"){
              $(_this).addClass('active');
              $(_this).attr('data-cId', data.info);
              return;
            } 
            lib.showMessage(data.info);
          }, function(){
            lib.showMessage('网络延时，请稍后再试');
          });
        }
      });
      /*立即支付*/
      $(document).on('tap', '.ui-go-pay', function(){
        location.href = `/order/confirm_order.html?mt=2&gId=${_t.goodsId}&sId=${_t.storageId}&gNum=${$('input[name=goodsNum]').val()}`;
      });
    },
    initPage: function(){
      var _t = this;
      lib.ajx(lib.getReq().ser + 'goods/getGoodsDetail.action', {goodsId: _t.goodsId}, function(data){
        if(data.infocode === "0"){
          var b = '<div class="swiper-wrapper">', h = '';
          $.each(data.info.albumList, function(k, v) {
          	b +=`<div class="swiper-slide">
                  <img class="lazy" src="${lib.getReq().imgPath + v.albumURL}" />
                </div>`
          });
          b += '</div><div class="swiper-pagination"></div>';
          $(".ui-banner").html(b);
          _t.initBanner();
          
          h += `<div class="ui-goods-name">
                  <i>正品</i>${data.info.chName}
                </div>
                <!--价格-->
                <div class="ui-goods-price">
                  <i class="ui-seller-price">￥${data.info.sellPrice}</i><del class="ui-origin-price" style="${Number(data.info.marketPrice) ? '' : 'display:none'}">￥${data.info.marketPrice}</del>
                </div>
                <!--价格-->
                <div class="ui-goods-other" style="${data.info.activeFlag ? '' : 'display:none'}">
                  <i class="ui-title">促销</i>${data.info.activeFlag}
                </div>
                <div class="ui-goods-other" style="${data.info.modelType ? '' : 'display:none'}">
                  <i class="ui-title">规格</i>${data.info.modelType}
                </div>
                <!--数量-->
                <div class="ui-goods-other">
                  <i class="ui-title">数量</i>
                  <div class="ui-g-num">
                    <i class="ui-plus"></i>
                    <input value="1" type="tel" name="goodsNum">
                    <i class="ui-add"></i>
                  </div>
                </div>
                <!--商品图-->
                <div class="ui-goods-other">
                  <i class="ui-title">商品详情</i>
                  <i class="ui-up-r" style="float: right;margin-top:0.66rem"></i>
                </div>
                <div class="ui-goods-img">`;
                $.each(data.info.richPicList, function(k, v) {
                  h += `<img src="${lib.getReq().imgPath + v.pictureUrl}" />`;
                	
                });
                `</div>`;
          $(".ui-goods-info").html(h);
        }else{
          lib.showMessage(data.info);
        }
      }, function(){
        lib.showMessage('网络延时，请稍后再试');
      })
    },
    isConllected: function(){
      var _t = this;
      lib.ajx(lib.getReq().ser + 'memberCollectGoods/isConllected.action', {goodsId: _t.goodsId, storageId: _t.storageId}, function(data){
        if(data.infocode === '0'){
          $(".ui-add-collect").addClass('active');
          $(".ui-add-collect").attr('data-cId', data.info);
        }
      }, function(){
        lib.showMessage('网络延时，请稍后再试');
      });
    }
  });
  fn.init();
});
